package com.engine4cloud.tdf.supervision.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.engine4cloud.tdf.common.core.constant.SupervisionBusinessConstants;
import com.engine4cloud.tdf.common.core.constant.enums.DictNameEnum;
import com.engine4cloud.tdf.common.security.util.SecurityUtils;
import com.engine4cloud.tdf.supervision.convert.JzdxEmploymentConvert;
import com.engine4cloud.tdf.supervision.dto.create.JzdxEmploymentCreateDTO;
import com.engine4cloud.tdf.supervision.dto.result.DictSelectResultDTO;
import com.engine4cloud.tdf.supervision.dto.result.JzdxEmploymentResultDTO;
import com.engine4cloud.tdf.supervision.dto.update.JzdxEmploymentUpdateDTO;
import com.engine4cloud.tdf.supervision.entity.JzdxEmploymentEntity;
import com.engine4cloud.tdf.supervision.mapper.JzdxEmploymentMapper;
import com.engine4cloud.tdf.supervision.service.DictSelectService;
import com.engine4cloud.tdf.supervision.service.JzdxEmploymentService;
import jakarta.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 矫正对象就业就学信息
 *
 * @author wenjie
 * @date 2024-04-03 14:48:44
 */
@Service
public class JzdxEmploymentServiceImpl extends ServiceImpl<JzdxEmploymentMapper, JzdxEmploymentEntity> implements JzdxEmploymentService {

    @Resource
    private DictSelectService dictSelectService;

    @Override
    public JzdxEmploymentResultDTO find(String code) {
        JzdxEmploymentResultDTO employment = this.baseMapper.findByCode(code);
        if (Objects.nonNull(employment)) {
            this.setPreProfession(employment);
        }
        return employment;
    }

    @Override
    public Boolean create(JzdxEmploymentCreateDTO employment, String code) {
        JzdxEmploymentEntity entity = JzdxEmploymentConvert.INSTANCE.createList(employment);
        entity.setRybh(code);
        entity.setTenantId(SecurityUtils.getUser().getTenantId());
        return this.save(entity);
    }

    @Override
    public void setPreProfession(JzdxEmploymentResultDTO result) {
        List<DictSelectResultDTO> professionList = dictSelectService.items(DictNameEnum.PREVIOUS_JOB.getType());
        if (CollectionUtils.isEmpty(professionList)) {
            return;
        }

        List<String> preProfessionList = new ArrayList<>();
        Map<String, DictSelectResultDTO> preProfessionMap = professionList.stream().collect(Collectors.toMap(DictSelectResultDTO::getCode, v -> v));

        String profession = result.getPqzy();
        while (!Objects.equals(SupervisionBusinessConstants.AREA_ROOT_CODE, profession) && preProfessionMap.containsKey(profession)) {
            preProfessionList.add(profession);
            DictSelectResultDTO currentProfession = preProfessionMap.get(profession);
            profession = currentProfession.getPcode();
        }

        ///翻转，父在前，子在后
        Collections.reverse(preProfessionList);
        result.setPreProfessionList(preProfessionList);
    }

    @Override
    public Boolean updateByCode(JzdxEmploymentUpdateDTO dto, String code) {
        JzdxEmploymentResultDTO result = this.find(code);
        if (Objects.isNull(result)) {
            return this.create(JzdxEmploymentConvert.INSTANCE.updateCreate(dto), code);
        }

        dto.setId(result.getId());
        return this.updateById(JzdxEmploymentConvert.INSTANCE.update(dto));
    }
}